/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.html.xhtml;

import java.util.Locale;

import org.junit.Assert;
import org.junit.Test;

import pikes.uri.RelativeURI;
import pikes.xml.XMLTag;

public class HtmlTagsTestCase {

	@Test
	public void testHtmlLanguage() throws Exception {
		Html html = new Html(new Head(new Title("title")), new Body());
		html.setLang(new Locale("en"));
		XMLTag expected = new XMLTag("html");

		XMLTag expectedHead = new XMLTag("head");
		XMLTag titleTag = new XMLTag("title");
		titleTag.addText("title");
		expectedHead.addTag(titleTag);
		expected.addTag(expectedHead);

		expected.addTag(new XMLTag("body"));
		expected.addAttribute("lang", "en");
		Assert.assertEquals(expected, html.createTag());
	}
	
	@Test
	public void testHtmlDirection() throws Exception {
		Html html = new Html(new Head(new Title("title")), new Body());
		html.setDir(Direction.LEFT_TO_RIGHT);
		XMLTag expected = new XMLTag("html");

		XMLTag expectedHead = new XMLTag("head");
		XMLTag titleTag = new XMLTag("title");
		titleTag.addText("title");
		expectedHead.addTag(titleTag);
		expected.addTag(expectedHead);

		expected.addTag(new XMLTag("body"));
		expected.addAttribute("dir", "ltr");
		Assert.assertEquals(expected, html.createTag());
	}
	
	@Test
	public void testHead() throws Exception {
		Head head = new Head(new Title("title1"));
		XMLTag expected = new XMLTag("head");
		XMLTag titleTag = new XMLTag("title");
		titleTag.addText("title1");
		expected.addTag(titleTag);
		Assert.assertEquals(expected, head.createTag());
	}
	
	@Test
	public void testBody() throws Exception {
		Body body = new Body();
		Assert.assertEquals(new XMLTag("body"), body.createTag());
	}
	
	@Test
	public void testBodySetBlock() throws Exception {
		Body body = new Body();
		body.add(new Block() {

			public void decorateTag(XMLTag tag) {
				tag.addTag(new XMLTag("boo"));

			}

		});
		XMLTag expected = new XMLTag("body");
		expected.addTag(new XMLTag("boo"));
		Assert.assertEquals(expected, body.createTag());
	}
	
	@Test
	public void testHtmlSetBodyAndHead() throws Exception {
		Html html = new Html(new Head(new Title("title")), new Body());

		XMLTag expected = new XMLTag("html");

		XMLTag expectedHead = new XMLTag("head");
		XMLTag titleTag = new XMLTag("title");
		titleTag.addText("title");
		expectedHead.addTag(titleTag);
		expected.addTag(expectedHead);

		expected.addTag(new XMLTag("body"));

		Assert.assertEquals(expected, html.createTag());
	}
	
	@Test
	public void testMeta() throws Exception {
		Meta meta = new Meta("text");
		XMLTag expected = new XMLTag("meta");
		expected.addAttribute("content", "text");
		Assert.assertEquals(expected, meta.createTag());
	}
	
	@Test
	public void testMetaHttpEquiv() throws Exception {
		Meta meta = new Meta("text");
		meta.setHTTPEquiv("Content-Type");
		XMLTag expected = new XMLTag("meta");
		expected.addAttribute("content", "text");
		expected.addAttribute("http-equiv", "Content-Type");
		Assert.assertEquals(expected, meta.createTag());
	}
	
	@Test
	public void testText() throws Exception {
		XMLTag tag = new XMLTag("link");
		PCData text = new PCData("text");
		text.decorateTag(tag);
		XMLTag expected = new XMLTag("link");
		expected.addText("text");
		Assert.assertEquals(expected, tag);
	}
	
	@Test
	public void testText1() throws Exception {
		XMLTag tag = new XMLTag("link");
		PCData text = new PCData("text");
		text.addText("text2");
		text.decorateTag(tag);
		XMLTag expected = new XMLTag("link");
		expected.addText("texttext2");
		Assert.assertEquals(expected, tag);
	}
	
	@Test
	public void testLink() throws Exception {
		Link link = new Link();
		XMLTag expected = new XMLTag("link");
		Assert.assertEquals(expected, link.createTag());
	}
	
	@Test
	public void testLink1() throws Exception {
		Link link = new Link();
		link.setRel("stylesheet");
		link.setHRef("/countries/themes/spring/spring.css");
		link.setType("text/css");
		XMLTag expected = new XMLTag("link");
		expected.addAttribute("rel", "stylesheet");
		expected.addAttribute("href", "/countries/themes/spring/spring.css");
		expected.addAttribute("type", "text/css");
		Assert.assertEquals(expected, link.createTag());
	}
	
	@Test
	public void testDivision() throws Exception {
		Division div = new Division();
		XMLTag expected = new XMLTag("div");
		Assert.assertEquals(expected, div.createTag());
	}
	
	@Test
	public void testSpan() throws Exception {
		Span span = new Span();
		XMLTag expected = new XMLTag("span");
		Assert.assertEquals(expected, span.createTag());
	}
	
	@Test
	public void testImage() throws Exception {
		Image image = new Image(new RelativeURI("uri"), "text");
		XMLTag expected = new XMLTag("img");
		expected.addAttribute("src", "/uri");
		expected.addAttribute("alt", "text");
		Assert.assertEquals(expected, image.createTag());
	}
	
	@Test
	public void testImage1() throws Exception {
		Image image = new Image("uri", "text");
		XMLTag expected = new XMLTag("img");
		expected.addAttribute("src", "/uri");
		expected.addAttribute("alt", "text");
		Assert.assertEquals(expected, image.createTag());
	}
	
	@Test
	public void testTitle() throws Exception {
		Title title = new Title("boo");
		title.setDir(Direction.LEFT_TO_RIGHT);
		XMLTag expected = new XMLTag("title");
		expected.addAttribute("dir", "ltr");
		expected.addText("boo");
		Assert.assertEquals(expected, title.createTag());
	}
}
